﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Vds2465Telegrams
{
	public enum eDirection
	{
		/// <summary>
		/// ÜG nach ÜZ (Kommend / Ein / Ausgelöst / Offen)
		/// </summary>
		ATE_to_RCT,

		/// <summary>
		/// ÜZ nach ÜG (Gegangen / Aus / Zurückgesetzt / Geschlossen)
		/// </summary>
		RCT_to_ACE
	}

	public abstract class Telegram
	{
		#region private member variables

		private readonly eDirection _direction;
		private readonly byte _address;

		#endregion private member variables
		
		#region constructor

		public Telegram(eDirection direction, byte address)
		{
			_direction = direction;
			_address = address;
		}

		#endregion constructor

		#region public getters

		public eDirection Direction { get { return _direction; } }
		public byte Address { get { return _address; } }

		#endregion public getters

		#region ToString, Equals, GetHashCode

		public override int GetHashCode()
        {
            int hashCode = Address;
            if (Direction == eDirection.ATE_to_RCT)
            {
                hashCode += 0x100;
            }

            return hashCode;
        }

        public override string ToString()
        {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append("(");
            stringBuilder.Append(ToStringProperties());
            stringBuilder.Append(")");

            return stringBuilder.ToString();
        }

		internal virtual string ToStringProperties()
        {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append("Direction='");
            if (Direction == eDirection.ATE_to_RCT)
            {
                stringBuilder.Append("ATE_to_RCT");
            }
            else
            {
                stringBuilder.Append("RCT_to_ACE");
            }
            stringBuilder.Append("', ");
            stringBuilder.Append("Address='");
            stringBuilder.Append(Address);
            stringBuilder.Append("'");
            
            return stringBuilder.ToString();
        }

        public override bool Equals(object obj)
        {
            Type objType = obj.GetType();
            if (this.GetType() == objType)
            {
                Telegram telegram2Compare = obj as Telegram;
                return EqualsProperties(telegram2Compare);
            }

            return false;
        }

		internal bool EqualsProperties(Telegram telegram2Compare)
        {
            if (this.Direction == telegram2Compare.Direction)
            {
                return (this.Address == telegram2Compare.Address);
            }

            return false;
		}

		#endregion ToString, Equals, GetHashCode
	}
}
